package cloud.topdaddy.admin.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cloud.topdaddy.admin.system.entity.SysPost;
import cloud.topdaddy.admin.system.entity.SysUser;
import cloud.topdaddy.admin.system.service.SysPostService;
import cloud.topdaddy.admin.system.service.SysUserService;
import cloud.topdaddy.base.common.resp.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/sysPost")
public class SysPostController {

    @Resource
    private SysPostService sysPostService;
    @Resource
    private SysUserService sysUserService;

    @GetMapping("/list")
    @SaCheckLogin
    @SaCheckPermission("system:post:list")
    public R<IPage<SysPost>> list(@RequestParam(name = "pageNo", defaultValue = "1") long pageNo,
                                  @RequestParam(name = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(name = "positionName", required = false) String positionName,
                                  @RequestParam(name = "positionLevel", required = false) String positionLevel) {
        Page<SysPost> page = new Page<>(pageNo, pageSize);
        var wrapper = sysPostService.lambdaQuery();
        if (StringUtils.isNotBlank(positionName)) {
            wrapper.like(SysPost::getPositionName, positionName);
        }
        if (StringUtils.isNotBlank(positionLevel)) {
            wrapper.eq(SysPost::getPositionLevel, positionLevel);
        }
        wrapper.orderByAsc(SysPost::getSort);
        IPage<SysPost> result = wrapper.page(page);
        return R.ok(result);
    }

    @GetMapping("/{positionId}")
    @SaCheckLogin
    public R<SysPost> get(@PathVariable("positionId") Long positionId) {
        return R.ok(sysPostService.getById(positionId));
    }

    @PostMapping("/add")
    @SaCheckLogin
    @SaCheckPermission("system:post:add")
    public R<String> add(@RequestBody @Valid SysPost post) {
        post.setCreateTime(LocalDateTime.now());
        sysPostService.save(post);
        return R.ok("新增成功");
    }

    @PutMapping("/update")
    @SaCheckLogin
    @SaCheckPermission("system:post:update")
    public R<String> update(@RequestBody @Valid SysPost post) {
        post.setUpdateTime(LocalDateTime.now());
        sysPostService.updateById(post);
        return R.ok("更新成功");
    }

    @DeleteMapping("/delete")
    @SaCheckLogin
    @SaCheckPermission("system:post:delete")
    public R<String> delete(@RequestParam("ids") List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return R.userErrorParam("ids 不能为空");
        }
        // 校验是否有用户引用这些岗位/职务
        long refCount = sysUserService.lambdaQuery().in(SysUser::getPositionId, ids).count();
        if (refCount > 0) {
            return R.userErrorParam("存在用户引用所选职务，无法删除");
        }
        sysPostService.removeBatchByIds(ids);
        return R.ok("删除成功");
    }
}
